home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / ashc5.arc / DOHC5.C < prev    next >
C/C++ Source or Header  |  1990-12-24  |  5KB  |  231 lines

  1. /*
  2.  *      MC6805 specific processing
  3.  */
  4.  
  5. /* addressing modes */
  6. #define IMMED   0       /* immediate */
  7. #define IND     1       /* indexed */
  8. #define OTHER   2       /* NOTA */
  9.  
  10. /*
  11.  *      localinit --- machine specific initialization
  12.  */
  13. localinit()
  14. {
  15. }
  16.  
  17. /*
  18.  *      do_op --- process mnemonic
  19.  *
  20.  *    Called with the base opcode and it's class. Optr points to
  21.  *    the beginning of the operand field.
  22.  */
  23. do_op(opcode,class)
  24. int opcode;    /* base opcode */
  25. int class;    /* mnemonic class */
  26. {
  27.     char *skip_white();
  28.     int     dist;   /* relative branch distance */
  29.     int     amode;  /* indicated addressing mode */
  30.     char    *peek;
  31.  
  32.     /* guess at addressing mode */
  33.     peek = Optr;
  34.     amode = OTHER;
  35.     if( head(Operand,"X") || head(Operand,"x") ) {
  36.         if( delim(*(Operand+1)) )
  37.             amode = IND;
  38.         }
  39.     else while( !delim(*peek) ) { /* check for comma in operand field */
  40.         if( *peek++ == ',' ){
  41.             amode = IND;
  42.             break;
  43.             }
  44.         }
  45.     if( *Optr == '#' ) amode = IMMED;
  46.  
  47.     switch(class){
  48.         case INH:                       /* inherent addressing */
  49.             emit(opcode);
  50.             return;
  51.         case GEN:                       /* general addressing */
  52.             do_gen(opcode,amode);
  53.             return;
  54.         case REL:                       /* short relative branches */
  55.             if( eval() )
  56.                 dist = Result - (Pc+2);
  57.             else dist = -2;
  58.             emit(opcode);
  59.             if( (dist >127 || dist <-128) && Pass==2){
  60.                 error("Branch out of Range");
  61.                 dist = -2;
  62.                 }
  63.             emit(lobyte(dist));
  64.             return;
  65.         case NOIMM:
  66.             if( amode == IMMED ){
  67.                 error("Immediate Addressing Illegal");
  68.                 return;
  69.                 }
  70.             do_gen(opcode,amode);
  71.             return;
  72.         case GRP2:
  73.             if( amode == IND ){
  74.                 do_indexed(opcode+0x20);
  75.                 return;
  76.                 }
  77.             eval();
  78.             Cycles += 1;
  79.             if(Force_byte){
  80.                 emit(opcode);
  81.                 emit(lobyte(Result));
  82.                 return;
  83.                 }
  84.             if(Result>=0 && Result <=0xFF){
  85.                 emit(opcode);
  86.                 emit(lobyte(Result));
  87.                 return;
  88.                 }
  89.             error("Extended Addressing not allowed");
  90.             return;
  91.         case SETCLR:
  92.         case BTB:
  93.             eval();
  94.             if(Result <0 || Result >7){
  95.                 error("Bit Number must be 0-7");
  96.                 return;
  97.                 }
  98.             emit( opcode | (Result << 1));
  99.             if(*Optr++ != ',') error("Missing comma");
  100.             Optr = skip_white(Optr);
  101.             eval();
  102.             emit(lobyte(Result));
  103.             if(class==SETCLR)
  104.                 return;
  105.             /* else it's bit test and branch */
  106.             if(*Optr++ != ',') error("Missing comma");
  107.             Optr = skip_white(Optr);
  108.             if( eval() )
  109.                 dist = Result - (Old_pc+3);
  110.             else dist = -3;
  111.             if( (dist >127 || dist <-128) && Pass==2){
  112.                 error("Branch out of Range");
  113.                 dist = -3;
  114.                 }
  115.             emit(lobyte(dist));
  116.             return;
  117.         default:
  118.             fatal("Error in Mnemonic table");
  119.         }
  120. }
  121.  
  122. /*
  123.  *      do_gen --- process general addressing
  124.  */
  125. do_gen(op,mode)
  126. int     op;
  127. int     mode;
  128. {
  129.     if( mode == IMMED){
  130.         Optr++;
  131.         emit(op);
  132.         eval();
  133.         emit(lobyte(Result));
  134.         return;
  135.         }
  136.     else if( mode == IND ){
  137.         do_indexed(op+0x30);
  138.         return;
  139.         }
  140.     else if( mode == OTHER){        /* direct or extended addressing */
  141.         eval();
  142.         if(Force_word){
  143.             emit(op+0x20);
  144.             eword(Result);
  145.             Cycles += 2;
  146.             return;
  147.             }
  148.         if(Force_byte){
  149.             emit(op+0x10);
  150.             emit(lobyte(Result));
  151.             Cycles += 1;
  152.             return;
  153.             }
  154.         if(Result >= 0 && Result <= 0xFF){
  155.             emit(op+0x10);
  156.             emit(lobyte(Result));
  157.             Cycles += 1;
  158.             return;
  159.             }
  160.         else {
  161.             emit(op+0x20);
  162.             eword(Result);
  163.             Cycles += 2;
  164.             return;
  165.             }
  166.         }
  167.     else {
  168.         error("Unknown Addressing Mode");
  169.         return;
  170.         }
  171. }
  172.  
  173. /*
  174.  *      do_indexed --- handle all weird stuff for indexed addressing
  175.  */
  176. do_indexed(op)
  177. int op;
  178. {
  179.     if( *Optr != ',' && !head(Operand,"X") && !head(Operand,"x") ) {
  180.         eval();
  181.             if( !( *Optr++ == ',' && ( mapdn(*Optr) == 'x') ) )
  182.             warn("Indexed Addressing Assumed");
  183.         }
  184.     else {            /* if no expression, fake it */
  185.         Force_word = NO;
  186.         Force_byte = NO;
  187.         Result = 0;
  188.         if( *Optr == ',' ) Optr++;
  189.         if( mapdn(*Optr) != 'x' )
  190.             warn("Indexed Addressing Assumed");
  191.         }
  192.     if(Force_word){
  193.         if(op < 0x80 ){ /* group 2, no extended addressing */
  194.             emit(op+0x10); /* default to one byte indexed */
  195.             emit(lobyte(Result));
  196.             Cycles += 2;
  197.             return;
  198.             }
  199.         emit(op);
  200.         eword(Result);
  201.         Cycles += 3;
  202.         return;
  203.         }
  204.     Cycles += 2;    /* assume 1 byte indexing */
  205.     if(Force_byte){
  206.         emit(op+0x10);
  207.         emit(lobyte(Result));
  208.         return;
  209.         }
  210.     if(Result==0){
  211.         emit(op+0x20);
  212.         Cycles--;       /* ,x slightly faster */
  213.         return;
  214.         }
  215.     if(Result>0 && Result <=0xFF){
  216.         emit(op+0x10);
  217.         emit(lobyte(Result));
  218.         return;
  219.         }
  220.     if( op < 0x80 ){
  221.         warn("Address Offset Value Truncated");
  222.         emit(op+0x10);
  223.         emit(lobyte(Result));
  224.         return;
  225.         }
  226.     emit(op);
  227.     eword(Result);
  228.     Cycles++;       /* 2 byte slightly slower */
  229.     return;
  230. }
  231.